package service

import (
	"fmt"
	"strconv"
	"zhonghui/console/warehouse/cfgx"
	"zhonghui/console/warehouse/logx"

	jsoniter "github.com/json-iterator/go"

	"github.com/ddliu/go-httpclient"
)

type Departments struct {
	BaseResp
	Department []DepartmentList `json:"department"`
}

type DepartmentList struct {
	//DepartmentLeader []interface{} `json:"department_leader"`
	ID       int    `json:"id"`
	Name     string `json:"name"`
	Order    int64  `json:"order"`
	Parentid int    `json:"parentid"`
}

func GetDepartmentList(accessToken string) (*Departments, error) {
	departments := Departments{}
	host := cfgx.Cfg().GetString("wx.host")
	url := fmt.Sprintf("%s/cgi-bin/department/list", host)
	res, err := httpclient.Get(url, map[string]string{
		"access_token": accessToken,
	})
	if err != nil {
		return nil, err
	}
	ret, err := res.ToString()
	if err != nil {
		return nil, err
	}
	return &departments, jsoniter.UnmarshalFromString(ret, &departments)
}

type Employees struct {
	BaseResp
	UserList []Employee `json:"userlist"`
}

type Employee struct {
	Department []int  `json:"department"`
	Name       string `json:"name"`
	OpenUserid string `json:"open_userid"`
	Userid     string `json:"userid"`
}

func GetEmployeeList(accessToken string, id int) (*Employees, error) {
	employees := Employees{}
	host := cfgx.Cfg().GetString("wx.host")
	url := fmt.Sprintf("%s/cgi-bin/user/simplelist", host)
	res, err := httpclient.Get(url, map[string]string{
		"access_token":  accessToken,
		"department_id": strconv.Itoa(id),
	})
	if err != nil {
		return nil, err
	}
	ret, err := res.ToString()
	if err != nil {
		return nil, err
	}
	return &employees, jsoniter.UnmarshalFromString(ret, &employees)
}

type EmployeeDetail struct {
	BaseResp
	Name   string `json:"name"`
	Mobile string `json:"mobile"`
}

func GetEmployeeDetail(accessToken string, user_id string) (*EmployeeDetail, error) {
	employeeDetail := EmployeeDetail{}
	host := cfgx.Cfg().GetString("wx.host")
	url := fmt.Sprintf("%s/cgi-bin/user/get", host)
	res, err := httpclient.Get(url, map[string]string{
		"access_token": accessToken,
		"userid":       user_id,
	})
	if err != nil {
		return nil, err
	}
	ret, err := res.ToString()
	if err != nil {
		return nil, err
	}
	logx.Zap().Info("GetEmployeeDetail", "ret", ret)
	return &employeeDetail, jsoniter.UnmarshalFromString(ret, &employeeDetail)
}

type GetEmployeeActiveListRequest struct {
	Limit int `json:"limit"`
}
type GetEmployeeActiveListItem struct {
	OpenUserid string `json:"open_userid"`
}
type GetEmployeeActiveListResponse struct {
	BaseResp
	NextCursor     string                      `json:"next_cursor"`
	MemberAuthList []GetEmployeeActiveListItem `json:"member_auth_list"`
}

func GetEmployeeActiveList(accessToken string) (*GetEmployeeActiveListResponse, error) {
	req := GetEmployeeActiveListRequest{
		Limit: 1000,
	}
	resp := GetEmployeeActiveListResponse{}
	host := cfgx.Cfg().GetString("wx.host")
	url := fmt.Sprintf("%s/cgi-bin/user/check_member_auth?access_token=%s", host, accessToken)
	jsonBody, err := jsoniter.MarshalToString(req)
	if err != nil {
		return nil, err
	}
	res, err := httpclient.PostJson(url, jsonBody)
	if err != nil {
		return nil, err
	}
	ret, err := res.ToString()
	if err != nil {
		return nil, err
	}
	logx.Zap().Infow("GetEmployeeActiveList", "ret", ret)
	return &resp, jsoniter.UnmarshalFromString(ret, &resp)
}
